package com.zpc.myvideoserver.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zpc.myvideoserver.common.util.RedisUtil;
import com.zpc.myvideoserver.common.util.ResponseJson;
import com.zpc.myvideoserver.entity.FileMessage;
import com.zpc.myvideoserver.entity.Video;
import com.zpc.myvideoserver.entity.Vo.VideoVo;
import com.zpc.myvideoserver.service.GetFileInfoService;
import com.zpc.myvideoserver.service.UploadFileService;
import com.zpc.myvideoserver.service.VideoService;
import com.zpc.myvideoserver.service.VideoUrlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * <p>
 * 视频信息管理表 前端控制器
 * </p>
 *
 * @author zpc
 * @since 2023/11/03
 */
@RestController
@RequestMapping("/video")
@Api(value = "/video",tags = "视频管理")
public class VideoController {

       @Autowired
       VideoService videoService;
       @Autowired
       UploadFileService uploadFileService;
       @Autowired
       GetFileInfoService  getFileInfoService;
       @Autowired
       VideoUrlService videoUrlService;
       @ApiOperation(value = "上传视频",notes="上传视频及其封面")
       @PostMapping("insert")
       public ResponseJson<Video> insert(
               @RequestPart(value = "videofile") MultipartFile videofile,
               @RequestPart(value = "cover") MultipartFile cover,
               @RequestParam(value = "videoTitle") String title,
               @RequestParam(value = "videoClassify") String classify
               )
       {
           Video hasvideo = videoService.getByVideoName(videofile.getOriginalFilename());
           if(hasvideo==null)
           {
               Video video = new Video();
               video.setVideoTitle(title);
               video.setVideoName(videofile.getOriginalFilename());
               video.setCoverName(cover.getOriginalFilename());
               video.setVideoClassify(classify);
               boolean resvideo = uploadFileService.upload(videofile,"myvideo/video/"+videofile.getOriginalFilename());
               boolean rescover=  uploadFileService.upload(cover,"myvideo/cover/"+cover.getOriginalFilename());
               boolean resinsert = videoService.save(video);
               if(rescover&&resinsert&&resvideo){
                   Video revideo = videoService.getByVideoName(videofile.getOriginalFilename());
                   // @Description : 获取连接列表 zpc20 2023/11/6 11:42
                   Map<String,Object> urllist = getFileInfoService.GetVideoFiles(revideo,300);
                   // @Description : 存入redis zpc20 2023/11/6 11:41
                   boolean storeUrl = videoUrlService.saveByHashExpire(revideo.getVideoName()+":"+revideo.getId(),urllist,300);
                   if(storeUrl){
                       return ResponseJson.SuccessResponse("上传成功!",video);
                   }else {
                     return   ResponseJson.FailResponse("上传失败!",null);
                   }
               }else {
                   return ResponseJson.FailResponse("上传失败!",null);
               }
           }else {
               return new ResponseJson<>("文件名重复!请重新上传!",403,null);
           }
       }

    /**
     * @Name: getAllVideo
     * @Params : []:[]
     * @Description : 获取所有video
     * @Author : zpc20
     * @Date : 2023/11/6 11:29
     * @Return com.zpc.myvideoserver.common.util.ResponseJson<java.util.List<com.zpc.myvideoserver.entity.Vo.VideoVo>>
     * @Throws
     */
    @ApiOperation(value = "获取所有video",notes = "获取所有video url连接")
    @GetMapping("/getAllVideo")
    public ResponseJson<List<VideoVo>> getAllVideo(){
        List<Video> videoList=videoService.list(Wrappers.lambdaQuery());
        List<VideoVo> videoVoList = new ArrayList<>();
        // System.out.println(videoList.size());
        // BeanUtils.copyProperties(videos,videoVo);
        for (int i = 0; i < videoList.size(); i++) {
            Video video = videoList.get(i);
            VideoVo videoVo = new VideoVo();
            BeanUtils.copyProperties(video,videoVo);
            String hashKey = video.getVideoName()+":"+video.getId();
            Collection<Object> keys = new ArrayList<>();
            keys.add("videoUrl");
            keys.add("coverUrl");
            List<Object> urlList = videoUrlService.getAllUrl(hashKey,keys);
            if(urlList.get(0)==null){
                // System.out.println("~~~~~~~~~~~");
                // @Description : 获取连接 zpc20 2023/11/6 12:09
                Map<String,Object> urlMap =  getFileInfoService.GetVideoFiles(video,3600);
                // @Description : 若redis中不存在则存储 zpc20 2023/11/6 11:45
                videoUrlService.saveByHashExpire(hashKey,urlMap,3600);
                System.out.println(videoUrlService.getAllUrl(hashKey,keys).get(0));
                videoVo.setVideoUrl((String) videoUrlService.getAllUrl(hashKey,keys).get(0));
                videoVo.setCoverUrl((String) videoUrlService.getAllUrl(hashKey,keys).get(1));
                videoVoList.add(videoVo);
            }else {
                videoVo.setVideoUrl((String) urlList.get(0));
                videoVo.setCoverUrl((String) urlList.get(1));
                videoVoList.add(videoVo);
            }
        }
        return ResponseJson.SuccessResponse("查询成功",videoVoList);
    }

    @ApiOperation(value = "Id获取数据",notes = "通过ID获取video url连接")
    @GetMapping("/getVideoById")
    public ResponseJson<VideoVo> getVideoById(@RequestParam(value = "videoId") Integer videoId) {
        Video video=videoService.getById(videoId);
        VideoVo videoVo = new VideoVo();
        BeanUtils.copyProperties(video,videoVo);
        String hashKey = video.getVideoName()+":"+video.getId();
        Collection<Object> keys = new ArrayList<>();
        keys.add("videoUrl");
        keys.add("coverUrl");
        List<Object> urlList = videoUrlService.getAllUrl(hashKey,keys);
        if(urlList.get(0)==null){
            // @Description : 获取连接 zpc20 2023/11/6 12:09
            Map<String,Object> urlMap =  getFileInfoService.GetVideoFiles(video,3600);
            // @Description : 若redis中不存在则存储 zpc20 2023/11/6 11:45
            videoUrlService.saveByHashExpire(hashKey,urlMap,3600);
            System.out.println(videoUrlService.getAllUrl(hashKey,keys).get(0));
            videoVo.setVideoUrl((String) videoUrlService.getAllUrl(hashKey,keys).get(0));
            videoVo.setCoverUrl((String) videoUrlService.getAllUrl(hashKey,keys).get(1));
        }else {
            videoVo.setVideoUrl((String) urlList.get(0));
            videoVo.setCoverUrl((String) urlList.get(1));
        }

        return ResponseJson.SuccessResponse("查询成功",videoVo);
    }

}
